package com.cc.controller;

import com.cc.service.IEbusersService;
import com.cc.entity.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

import com.cc.mapper.userMapper;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import  java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    User user;
@Autowired
private IEbusersService IEbusersService;

@RequestMapping("re")
public  String re(){
    return "front/register";
}
@RequestMapping("register")
public String register(User user,int birthyear,int birthmonth,int birthdays){
        Date birthday=new Date(birthyear-1900,birthmonth,birthdays);
        user.setBirthday(birthday);
        user.setStatus(1);
        int result=IEbusersService.save(user);
        if(result==0)
            return "manage/false";
        return "manage/manage-result";
}
@RequestMapping("/login")
public String login(HttpSession session,@RequestParam("userName")String id, @RequestParam("passWord")String password){
    System.out.println(id);
    System.out.println(password);
    User user=IEbusersService.login(id,password);
    System.out.println(user);
    if(user==null){
        return "front/login";
    }
    session.setAttribute("user",user);//登录信息写入session中
    return "front/index";
}

    @RequestMapping("/list")
    public ModelAndView List(@RequestParam(value = "page",defaultValue = "1")int page,
                             @RequestParam(value = "pagesize",defaultValue = "3")int pagesize){
        List<User> userList=IEbusersService.findByPage(page,pagesize);
        System.out.println(userList);
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",userList);
        mv.addObject("curpage",page);
        int totalNum=IEbusersService.total();
        double num=(double)totalNum/pagesize;
        int no=(int)Math.ceil(num);
        mv.addObject("finalpage",no);
        mv.setViewName("manage/user");
return mv;
    }
    @RequestMapping("save")
    public String save(){
        //int result=IEbusersService.save(user);
        return "manage/user-add";
    }
@RequestMapping("AddCheck")
public String AddCheck(User user,int birthyear,int birthmonth,int birthdays){
        Date birthday=new Date(birthyear-1900,birthmonth,birthdays);
        user.setBirthday(birthday);
        user.setStatus(1);
        int result=IEbusersService.save(user);
        if(result==0)
            return "manage/false";
        return "manage/manage-result";
}

    @RequestMapping("update")
    public ModelAndView update(@RequestParam(value = "id")String id){
        User user=IEbusersService.selectByid(id);
        String birthday=user.getBirthday().toString();
        String pattern="yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf=new SimpleDateFormat(pattern);
        String d=sdf.format(user.getBirthday());
        String year=d.substring(0,4);
        String month=d.substring(5,7);
        String days=d.substring(8,10);
        int i=Integer.parseInt(year);
        int j=Integer.parseInt(month);
        int n=Integer.parseInt(days);
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",user);
        mv.addObject("year",year);
        mv.addObject("month",month);
        mv.addObject("days",days);
        mv.addObject("i",i);
        mv.addObject("j",j);
        mv.addObject("n",n);
        mv.setViewName("manage/user-modify");
        return mv;
    }
    @RequestMapping("updateCheck")
    public String updateCheck(User user,String birthyears,String birthmonths,String birthda){
        int birthyear=Integer.parseInt(birthyears);
        int birthmonth=Integer.parseInt(birthmonths);
        int birthdays=Integer.parseInt(birthda);
        Date birthday=new Date(birthyear-1900,birthmonth,birthdays);
        user.setBirthday(birthday);
        int result=IEbusersService.update(user);
        if(result==0)
            return "manage/false";
    return "manage/manage-result";
    }
    @RequestMapping("/delete")
    public String delete(@RequestParam("id") String id){
        int result=IEbusersService.delete(id);
        if(result==0){
            return "manage/false";
        }

        return "manage/manage-result";
    }
}
